package com.wang.work;

import java.util.Arrays;

public class HomeWork {

	public static void main(String[] args) {
		System.out.println(getLeapyear(4));
		System.out.println(getPrimeNum(198666));
		System.out.println(getSpecialStr("sdfghjk"));
		try {
			System.out.println(getSpecialSentence("aaaaa eeeeee"));
		} catch (NotMyStringException e) {
			System.out.println("请按要求传入字符串！！！！！");
			e.printStackTrace();
		}
//		System.out.println(breakPwd(80000,"你好哈哈S#$%^&*wertyudfghj452"));
//		findPwd(1,new int[1],"");
	}
	
	/**
	 * 判断一年是否是闰年
	 * @param year
	 * @return
	 */
	public static String getLeapyear(int year){
		String str = "闰年";
		if(year % 400 == 0){
			return str;
		}else if(year % 4 == 0 && year % 100 != 0){
			return str;
		}else
			return "平年";
	}
	/**
	 * 求大于目标值得最小质数
	 * @param num
	 * @return
	 */
	public static int getPrimeNum(int num){
		for (int i = num; ; i++) {
			if(isPrime(i)) return i;
		}
	}
	/**
	 * 判断一个数是不是质数
	 * @param num
	 * @return
	 */
	public static boolean isPrime(int num){
		if(num == 0) return false;
		num = Math.abs(num);
		if(num < 4) return true;
		for (int i = 2; i < num; i++) {
			if(num % i == 0) return false; 
		}
		return true;
	}
	/**
	 * 字符串奇数位上大写
	 * @param str
	 * @return
	 */
	public static String getSpecialStr(String str){
		StringBuilder res = new StringBuilder(str);
		double j = Math.ceil((double)str.length()/2);
		for (int i = 0; i < j; i++) {
			res.setCharAt(2*i,Character.toUpperCase(res.charAt(2*i)));
		}
		return res.toString();
	}
	/**
	 * 将句子的每个字母大写
	 * @param str
	 * @return
	 * @throws NotMyStringException
	 */
	public static String getSpecialSentence(String str) throws NotMyStringException{
		str = str.trim();
		if(!str.matches("^([a-zA-Z]+ ?)+$")) {//+ 至少一次  ?等价于匹配长度{0,1}
			throw new NotMyStringException("字符串不合法！");
		};
		StringBuilder res = new StringBuilder(str);
		int i = 0;
		do{
			res.setCharAt(i,Character.toUpperCase(res.charAt(i)));
			i = str.indexOf(' ',i);
		}while(i++ > 0);
		return res.toString();
	}
	/**
	 * 牛皮题
	 * @param num
	 * @param src
	 * @return
	 */
	public static String breakPwd(int num,String src){
		StringBuilder str = new StringBuilder();
		char[] charArray = src.toCharArray();
		for (int i = 0,j = charArray.length; i < j; i++) {
			for (int j2 = 0; j2 <= num; j2++) {
				if((char)j2 == charArray[i]) {
					str.append(charArray[i]);
					break;
				}
			}
			if(str.length() == 0) return "没找到！";
		}
		return "您输入的是："+str.toString();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static String breakInt(int num,int bitMax,String src){
		
		return "";
	}
	public static String findPwd(int num,int[] bitArr,String src) {
		System.out.println(Arrays.toString(bitArr));
		if(check(bitArr,src)){
			for (int i = 0,q = bitArr.length-1; i < q; i++) {
				int j = bitArr[i] + 1;
				if( j >= num){
					bitArr[i+1]++;
					bitArr[i] = 0;
					if(bitArr[q] >= num && bitArr[0] >= num) return "";
					bitArr[i]++;
					findPwd(num,bitArr,src);
				}else{
					bitArr[i]++;
					findPwd(num,bitArr,src);
				}
				System.out.println(Arrays.toString(bitArr));
			}
		}
		return "";
	}
	public static boolean check(int[] bitArr,String src){
		
		return false;
	}
}
